package com.example.question.tree.tree2;

import com.example.question.tree.tree1.Tree;
import com.example.question.tree.tree1.TreeNode;

public class Code10_124 {
    private TreeNode node;

    public static void main(String[] args) {
//        TreeNode node = Tree.createTree(new Integer[]{-2, 1, null, null, null});
        TreeNode node = Tree.createTree(new Integer[]{2, -1, null, null, null});
        Code10_124 code = new Code10_124();
        System.out.println("code.maxPathSum(node) = " + code.maxPathSum(node));
    }

    int ans = Integer.MIN_VALUE;
    // todo 再琢磨琢磨
    public int maxPathSum(TreeNode root) {
        if (root == null) {
            return 0;
        }
        maxSum(root);
        return ans;
    }

    private int maxSum(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int left = Math.max(0, maxSum(node.left));
        int right = Math.max(0, maxSum(node.right));

        ans = Math.max(ans, left + right + node.val);
        return Math.max(left, right) + node.val;

    }


}
